package org.skydiveapphd.ui;

import org.skydiveapphd.Constants;
import org.skydiveapphd.LogEntryEditActivity;
import org.skydiveapphd.LogEntryListActivity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class FragmentHelper {

	// singleton
	private static final FragmentHelper instance = new FragmentHelper();
	
	// closeRequests results
	private static final int ALLOWED = 10;
	private static final int DENIED = 20;
	private static final int POSTPONED = 30;
	
	private static Fragment requestedFragment;
	private static int requestedContainerViewId;
	private static String requestedFragTag;
	
	private FragmentHelper()
	{
	}
	
	public static FragmentHelper getInstance()
	{
		return instance;
	}
	
	public void openFragment(Fragment frag, int containterViewId, 
			String fragTag, FragmentManager fm)
	{
		// Create new fragment and transaction
		FragmentTransaction transaction = fm.beginTransaction();
		// Replace whatever is in the fragment_container view with this fragment,
		// and add the transaction to the back stack
		transaction.replace(containterViewId, frag, fragTag);
		transaction.addToBackStack(null);
		// Commit the transaction
		transaction.commit();
	}
	
	public static int requestFragment(Fragment frag, int containerViewId, 
			String fragTag, FragmentManager fm)
	{
		LogEntryEditActivity logEntryEdit = (LogEntryEditActivity) fm.findFragmentByTag(Constants.TAG_LOG_ENTRY_EDIT);
		
		// if the backstack contains 1 or more entries and one of them isn't a logEntryEditFragment (another activity is being performed)
		// OR if the backstack contains 2 or more entried and one of them is a logEntryEditFragment (a fragment deeper then the legEntryEdit is open)
		if ((fm.getBackStackEntryCount() >= 1 && logEntryEdit == null) || 
			(fm.getBackStackEntryCount() >= 2 && logEntryEdit != null ))
		{				
			return DENIED;
		}
		
		if (logEntryEdit == null)
		{
			// Create new fragment and transaction
			FragmentTransaction transaction = fm.beginTransaction();
			// Replace whatever is in the fragment_container view with this fragment,
			// and add the transaction to the back stack
			transaction.replace(containerViewId, frag, fragTag);
			transaction.addToBackStack(null);
			// Commit the transaction
			transaction.commit();
			
			return ALLOWED;
		}
		else
		{			
			if (logEntryEdit.requestClose())
			{	
				requestedFragment = frag;
				requestedContainerViewId = containerViewId;
				requestedFragTag = fragTag;
				
				return POSTPONED;
			}
			else
			{
				// Create new fragment and transaction
				FragmentTransaction transaction = fm.beginTransaction();
				// Replace whatever is in the fragment_container view with this fragment,
				// and add the transaction to the back stack
				transaction.replace(containerViewId, frag, fragTag);
				transaction.addToBackStack(null);
				// Commit the transaction
				transaction.commit();
				
				return ALLOWED;
			}
		}
	}
	
	public void notifyOfClose(FragmentManager fm)
	{
		if (requestedFragment != null)
		{
			((LogEntryListActivity) fm.findFragmentByTag(Constants.TAG_LOG_ENTRY_LIST)).highlightRequestedRow();
		
			// Create new fragment and transaction
			FragmentTransaction transaction = fm.beginTransaction();
			// Replace whatever is in the fragment_container view with this fragment,
			// and add the transaction to the back stack
			transaction.replace(requestedContainerViewId, requestedFragment, requestedFragTag);
			transaction.addToBackStack(null);
			// Commit the transaction
			transaction.commit();
			
			requestedFragment = null;
		}
	}
	
	public void cancelRequest()
	{
		requestedFragment = null;
	}
}